//
//  firstViewController.swift
//  WaiBao
//
//  Created by 赵佟越 on 17/5/11.
//  Copyright © 2017年 zhaotongyue. All rights reserved.
//

import UIKit
import DZNEmptyDataSet
import Moya
import SVProgressHUD
import SwiftyJSON
import RxSwift

class firstViewController: BaseViewController , UITableViewDataSource, UITableViewDelegate ,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate{
    var dataArr = NSArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeBaseUI()
        layoutNavigation()
        let myimageView = UIImageView()
        myimageView.sd_setImage(with: URL(string: "http://www.domain.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
        
        getData()
        
        
        // Do any additional setup after loading the view.
    }

    
    //MARK: - Private Methods
    func makeBaseUI(){
        SVProgressHUD.show()
        self.view.addSubview(self.myTableView)
    }
    func layoutNavigation(){
        self.title = "首页"
    }
    
    func getData(){
        let provider = RxMoyaProvider<MyService>()
        provider.request(.zen)
            .filterSuccessfulStatusCodes()
            .mapJSON()
            .subscribe(onNext: { (result) in
                print(result)
                let dic = JSON(result)["lists"][0]
                let Model = GongSiModel(json: dic)
                printLog(message: Model.hdphoto)
                self.dataArr = GongSiModel.transFormToArr(json: JSON(result)["lists"])
                self.myTableView.reloadData()
                
            }, onError: { (error) in
                printLog(message: JSON(error))
            })
            .addDisposableTo(disposeBag)
        
    }
    //MARK: - tableView Delegate And DataSource
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as?firstTableViewCell
        if cell == nil {
            cell = firstTableViewCell(style: .default, reuseIdentifier: "cell")
        }
        cell?.model = self.dataArr[indexPath.row] as! GongSiModel
        
//        if indexPath.section == 0{
//            let mytextView = UITextView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 40))
//            mytextView.font = UIFont.systemFont(ofSize: 17)
//            cell?.contentView .addSubview(mytextView)
//        }
        
        cell?.textLabel?.text = "1223"
        return cell!
    }
    //footer height
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    // row height
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 180
    }
    // header height
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    // row num
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    // 组数
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    //点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    override func willMove(toParentViewController parent: UIViewController?) {
        
    }
    // MARK : -DZ delegateAndDatesource
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let text = "No Application Found";
        return NSAttributedString.init(string: text)
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        return UIImage(imageLiteralResourceName: "人")
    }
    
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let text = String.init(format: "%@456", arguments: ["123"])
        let str = NSMutableAttributedString.init(string: text)
        //str.addAttribute(NSForegroundColorAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(0, 3))
        return  str
    }
    
    // MARK: - 懒加载
    private lazy var myTableView:UITableView = {
        let myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), style: UITableViewStyle.grouped)
        myTableView.delegate = self
        myTableView.dataSource = self
        myTableView.emptyDataSetDelegate = self
        myTableView.emptyDataSetSource = self
        
        myTableView.register(firstTableViewCell.self, forCellReuseIdentifier: "cell")
        return myTableView
    }()

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
